﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Guoc.Web.Controllers;
using Guoc.DAL.Interface;
using Guoc.DAL.Domain;
using System.Web.Mvc;
using Moq;
namespace Guoc.Web.Test.ControllerTest
{
    /// <summary>
    /// HomeControllerTest 的摘要说明
    /// </summary>
    [TestClass]
    public class HomeControllerTest
    {
        public HomeControllerTest()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，该上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        //
        // 编写测试时，可以使用以下附加特性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion
        [TestMethod]
        public void Login_Show_View()
        {
            var ac = new HomeController();
            var resultView = ac.Login();
            //断言
            Assert.IsNotNull(resultView);
            Assert.IsInstanceOfType(resultView, typeof(ViewResult), "View Instance is null");
            Assert.AreEqual("Login", ac.ViewBag.Title, "Page Title is Wrong");
        }

        [TestMethod]
        public void Login_UserName_And_Password_Are_Right()
        {
            string userName = "admin";
            string password = "123";
            string checkCode = "12345";

            var mockProvider = new Mock<IEmployee>();            
            //模拟调用接口对象
            mockProvider.Setup(e => e.GetEmployee(userName))
                        .Returns(new Employee() { LoginName="admin",LoginPwd="123"});

            //实际
            var ac = new HomeController(mockProvider.Object);
            var result = ac.Login(userName, password, checkCode);

            //断言
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            var route = result as RedirectToRouteResult;
            Assert.AreEqual("Index", route.RouteValues["action"], "Wrong Action");

            //验证虚拟异常
            mockProvider.VerifyAll();
        }

        [TestMethod]
        public void Login_should_return_error_if_userName_is_wrong() {
            string userName = "xxx";
            string password = "123";
            string checkCode = "12345";

            var mockProvider = new Mock<IEmployee>();
            //模拟调用接口对象
            mockProvider.Setup(e => e.GetEmployee(userName))
                        .Returns(new Employee() { LoginName = "admin", LoginPwd = "123" });

            //实际
            var ac = new HomeController(mockProvider.Object);
            var result = ac.Login(userName, password, checkCode);

            //断言
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(JsonResult));
           
            //验证虚拟异常
            mockProvider.VerifyAll();
        }

        [TestMethod]
        public void Login_should_return_error_if_passowrd_is_wrong()
        {
            string userName = "admin";
            string password = "989898";
            string checkCode = "12345";

            var mockProvider = new Mock<IEmployee>();
            //模拟调用接口对象
            mockProvider.Setup(e => e.GetEmployee(userName))
                        .Returns(new Employee() { LoginName = "admin", LoginPwd = "123" });

            //实际
            var ac = new HomeController(mockProvider.Object);
            var result = ac.Login(userName, password, checkCode);

            //断言
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(JsonResult));

            //验证虚拟异常
            mockProvider.VerifyAll();
        }


        [TestMethod]
        public void Login_should_return_error_if_userName_or_Password_is_missing()
        {
            string userName = "";
            string userName1 = null;
            HomeController hc = new HomeController();
            //出现错误，返回都是错误消息是json 形式  
            var result = hc.Login(userName, "123", "123456");
             Assert.IsInstanceOfType(result, typeof(JsonResult));
            result = hc.Login(userName1, "123", "123456");           
            Assert.IsInstanceOfType(result, typeof(JsonResult));
            
            string password = "";
            string password1 = null;
            result = hc.Login("admin", password, "123456");
            Assert.IsInstanceOfType(result, typeof(JsonResult));

            result = hc.Login("admin", password1, "123456");
            Assert.IsInstanceOfType(result, typeof(JsonResult));

        }


    }
}
